package com.wcity.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.BlockMapper;
import com.wcity.manage.dao.mapper.PropertyComplaintMapper;
import com.wcity.manage.dao.mapper.RegisterMapper;
import com.wcity.manage.dao.model.Block;
import com.wcity.manage.dao.model.Register;
import com.wcity.manage.dao.model.ComplaintManage;
import com.wcity.manage.service.property.PropertyComplaintService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 百姓心声管理
 * Created by zm on 2016/12/13.
 */
@Service
public class PropertyComplaintServiceImpl implements PropertyComplaintService {

    @Resource private PropertyComplaintMapper propertyComplaintMapper;
    @Resource private RegisterMapper registerMapper;
    @Resource private BlockMapper blockMapper;

    /**
     * 分页查询表单
     * @param pageModel
     * @return
     */
    @Override
    public List<ComplaintManage> getPayTypeListPage(PageModel pageModel) {
        return propertyComplaintMapper.findListPage(pageModel);
    }

    /**
     * 获取百姓心声
     * @param id
     * @return
     */
    @Override
    public ComplaintManage getComplaintManageInfo(String id) {
        return propertyComplaintMapper.getComplaintManageInfo( id );
    }

    /**
     * 回复
     * @param ComplaintManage
     * @return
     */
    @Override
    public int complaintManageEdit(ComplaintManage ComplaintManage) {
        return propertyComplaintMapper.update(ComplaintManage);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int replyDelete(String id) {
        return propertyComplaintMapper.delete(id);
    }

    /**
     * 查看回复内容
     * @param id
     * @param str
     * @return
     */
    @Override
    public ComplaintManage getComplaintManageInfo(String id, String str) {
        return propertyComplaintMapper.getReplyInfo(id,str);
    }

    /**
     * 修改状态
     * @param id
     * @param str
     * @return
     */
    @Override
    public int updateStatus(String id, String str) {
        return propertyComplaintMapper.updateStatus(id,str);
    }

    /**
     * 根据小区id获取百姓声音列表
     * @param pageModel
     * @return
     */
    @Override
    public Map<String, Object> getIComplaintManageList(PageModel pageModel) {
        Map<String,Object> map = new HashMap<>();
        List<Object> list = new ArrayList<>();
        try {
            List<ComplaintManage> ComplaintManageList = propertyComplaintMapper.findListPage(pageModel);
            System.out.println(ComplaintManageList.size());
            for ( ComplaintManage complaintManage:ComplaintManageList ) {
                Map<String,Object> mapObj = new HashMap<>();
                Register register = registerMapper.find( complaintManage.getRegId()  );
                mapObj.put("icon",register.getIcon());
                mapObj.put("complaintManage",complaintManage);
                list.add(mapObj);
            }
            System.out.println(JSON.toJSONString(list));
            for(Object m:list){
                Map<String,Object> m1 = (Map<String,Object>)m;
                ComplaintManage c = (ComplaintManage)m1.get("complaintManage");
                System.out.println("ComplaintManage,id："+c.getId());
            }
            map.put("list",list);
            map.put("retCode", Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 发布百姓声音
     * @param content
     * @param images
     * @return
     */
    @Override
    public String complaintManageAdd( String content , String images , String blockId , String regId ) {

        try{
            Register reg =  registerMapper.find(Integer.parseInt(regId));
            Block block = blockMapper.find(Integer.parseInt(blockId));
            if(reg == null || block == null){
                return "用户不存在";
            }
            String nickname = reg.getNickname();
            String phone = reg.getPhone();
            int userId = block.getUserId();
            String blockName = block.getBlockName();
            ComplaintManage ComplaintManage = new ComplaintManage();
            ComplaintManage.setBlockId(Integer.parseInt( blockId ));
            ComplaintManage.setCreateTime( new Date());
            ComplaintManage.setUserId( userId );
            ComplaintManage.setStatus( "未处理" );
            ComplaintManage.setAccountNum( nickname );
            ComplaintManage.setBlockName( blockName );
            ComplaintManage.setPhone( phone );
            ComplaintManage.setContent( content );
            ComplaintManage.setImages( images );
            ComplaintManage.setRegId( Integer.parseInt(regId) );

            int success = propertyComplaintMapper.insert( ComplaintManage );

            if( success == 1) {
                return "发布成功";
            }
        }catch (Exception e ){
            e.printStackTrace();
            return "服务器异常";
        }
        return "";
    }

    /**
     * 获取我发布的声音
     * @param regId
     * @return
     */
    @Override
    public List<ComplaintManage> getIComplaintManage(String regId) {
        return propertyComplaintMapper.getIComplaintManage (regId);
    }

    /**
     * 获取声音详情
     * @param ComplaintManageId
     * @return
     */
    @Override
    public ComplaintManage getIComplaintManageInfo(String ComplaintManageId) {
        return propertyComplaintMapper.getIComplaintManageInfo (ComplaintManageId);
    }
}
